<script type="text/javascript">
    layui.use(['form', 'layer', 'table', 'upload', 'laydate', 'jquery', 'element'], function () {
        $ = layui.jquery;
        var element = layui.element,
            layer = layui.layer,
            form = layui.form,
            table = layui.table,
            upload = layui.upload,
            laydate = layui.laydate;

        //公共方法form提交
        form.on('submit(submitIng)', function (data) {
            var url = $('form').data('url');
            $('button').addClass("layui-btn-disabled");//改成禁止点击样式
            $('button').attr("disabled", 'disabled');//禁止按钮点击

            $.ajax({
                url: url, type: 'post', data: data.field, dataType: "json",
                success: function (result) {
                   // console.log(result);return false;
                    if (result.code === 0) {
                        layer.msg(result.msg, {time: 1000, icon: 6}, function () {
                            var back = $('form').data('back');//web端自定义跳转路径
                            if(result.data.back){
                                back = result.data.back;//服务端返回的跳转路径
                            }
                            if (back) {
                                $(window).attr('location', back);
                            } else {
                                window.location.reload();
                            }
                        });
                    } else {
                        layer.msg(result.msg, {time: 2000, icon: 5}, function () {
                            $('button').attr("class", "layui-btn layui-btn-sm");
                            $('button').removeAttr("disabled");
                        });
                    }
                }
            });
            return false;
        });

        //公共方法检测该字段数据唯一性 （表单失去焦点事件）
        $(".checkOnly").blur(function () {
            var idObj = $("input[name='id']");//编辑数据的主键id
            var table = $(this).data('table');//表模型名称 例如（Admin）
            var id = idObj.val();
            var name = $(this).attr('name');  //检测的字段名称
            var val = $(this).val();          //表单输入的值
            var item = {table: table, id: id, key: name, val: val, '_token': '{{csrf_token()}}'};
            var that = $(this);
            if (table && name && val) {
                $.ajax({
                    url: "/checkOnly", type: 'post', data: item, dataType: "json",
                    success: function (result) {
                        if (result.code != 0) {
                            layer.msg(result.msg, {time: 1000, icon: 5}, function () {
                                that.val('');
                            });
                        }
                    }
                });
            }

            return false;
        });

        //上传图片
        upload.render({
            elem: '.uploadImg'
            , url: '{{route('public.uploadFile')}}'
            , accept: 'images'
            , acceptMime: 'image/jpg, image/png, image/jpeg'
            , data: {'_token': '{{csrf_token()}}', type: 'img'}
            , done: function (res, index, upload) {
                //上传成功
                if (res.code == 0) {
                    $('.shoImg').attr('src', res.data.url);
                    $('.shoImg').show();
                    $("#img_url").val(res.data.src);
                    layer.msg(res.msg, {time: 1000, icon: 6});
                } else {
                    layer.msg(res.msg, {time: 1000, icon: 5});
                }
            }
        });

        //查看大图
        $('.shoImg').on('click', function () {
            layer.photos({
                photos: '#layer-photos'
                , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });

    });

</script>
